<?php

class HttpMethod
{        
        const GET = 0;
        const POST = 1;
        const PUT = 2;        
        const DELETE = 3;
}

class WebException extends Exception {
    
}

class WSBParams {
	private $params=array();
	function add($name,$value) {
		$this->params[$name]=$value;
	}
	function toArray() {
		return $this->params;
	}
}

class WSBSoapParams {
    
    function removeProlog($text) {
        return str_replace('<?xml version="1.0"?>','',$text);
    }

	private $params=array();
	function add($name,$value) {
            if (is_array($value)) {
                $tmp='';
                foreach ($value as $each) {                    
                    $tmp.=$this->removeProlog($each->asXml());
                }
                $value=$tmp;
            } else if ($value instanceof ServiceModelBase) {
                throw new Exception('Not Support Yet');
            }
            $this->params[$name]=$value;
	}
	function toArray() {
		return $this->params;
	}
}

class WSBServiceBase {

	function createWSBQuery($customerKey,$forSoap) {
                if ($forSoap) {
                    $ans=new WSBSoapParams();    
                } else {
                    $ans=new WSBParams();
                }
		$ans->add('wsbck',$customerKey);
		return $ans;
	}
}

class WebResponse {
     function __construct($code, $header, $body) {
        $this->body = $body;
        $this->header = $header;
        $this->code = $code;		
    }
}

class WebClient {
         
    public static function request($url, $parameters, $httpMethod=0) {
        // prepare the request
        $ch = curl_init();
        
        if ($httpMethod == HttpMethod::GET) {
            $queryStr=http_build_query($parameters);
            $url=$url.(strpos($url,'?')?'&':'?').$queryStr;                                    
        } else if($httpMethod==HttpMethod::POST) {
            //throw new Exception('Not support yet');
            /*$data = HttpUtils::buildDataForContentType($contentType, $parameters, $headers);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $httpMethod);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);*/
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
            curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
        } else {
            throw new Exception('Not support yet');                
        }
        curl_setopt($ch, CURLOPT_URL, $url);        
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLINFO_HEADER_OUT, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        
        
        $body = curl_exec($ch);        
        if (curl_error($ch)) {
            throw new WebException(curl_error($ch));
        }
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        $header = curl_getinfo($ch, CURLINFO_HEADER_OUT);
        curl_close($ch);

        return new WebResponse($code, $header,$body);
    }

}

?>
